package com.zou.music_downloader.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zou.music_downloader.DownloadDialogFragment;
import com.zou.music_downloader.R;
import com.zou.music_downloader.bean.DataBean;
import com.zou.music_downloader.bean.SongListEntity;
import com.zou.music_downloader.sqlite.MyDao;
import com.zou.music_downloader.utils.ImageUtil;

import java.util.List;

/**
 * Created by zou on 2016/3/15.
 */
public class SongListAdapter extends RecyclerView.Adapter<SongListAdapter.SongViewHolder> {

    Context                            context;
    List<SongListEntity> datas;
    FragmentManager                    fragmentManager;
    Gson gson = new Gson();
    MyDao myDao;


    private final LayoutInflater mLayoutInflater;

    public SongListAdapter(Context context, List<SongListEntity> datas) {
        this.context = context;
        this.datas = datas;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public SongListAdapter(Context context, List<SongListEntity> datas, FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
        this.datas = datas;
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        myDao = new MyDao(context);

    }



    @Override
    public SongViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SongViewHolder(mLayoutInflater.inflate(R.layout.item_common_list_song, parent, false));
    }

    @Override
    public void onBindViewHolder(SongViewHolder holder, int position) {

        ImageUtil.load(datas.get(position).getPic_big(), context, holder.song_icon);
        holder.song_name.setText(datas.get(position).getTitle());
        holder.song_artist_name.setText(datas.get(position).getArtist_name());
        holder.song_album_name.setText(datas.get(position).getAlbum_title());
        holder.id = datas.get(position).getSong_id();


        // 标注已下载的歌曲
        DataBean dataBean = myDao.alterDateBySongId(datas.get(position).getSong_id());
        if (dataBean != null) {
            holder.if_downloaded.setText(dataBean.getState());
            holder.if_downloaded.setVisibility(View.VISIBLE);

            Log.e("wocao", "onBindViewHolder: fei null");

        } else {
            holder.if_downloaded.setVisibility(View.GONE);
            Log.e("wocao", "onBindViewHolder: null");

        }

    }

    @Override
    public int getItemCount() {
        if (datas == null) {
            return 0;
        }
        return datas.size();
    }


    class SongViewHolder extends RecyclerView.ViewHolder {
        String    id;
        TextView  song_name;
        TextView  song_album_name;
        TextView  song_artist_name;
        TextView  if_downloaded;
        ImageView song_icon;

        public SongViewHolder(View itemView) {
            super(itemView);


            song_name = (TextView) itemView.findViewById(R.id.song_name);
            song_album_name = (TextView) itemView.findViewById(R.id.song_album_name);
            song_artist_name = (TextView) itemView.findViewById(R.id.song_artist_name);
            if_downloaded = (TextView) itemView.findViewById(R.id.if_downloaded);
            song_icon = (ImageView) itemView.findViewById(R.id.song_icon);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("wocao", "onClick: " + id);

                    showDialog();

                }

                private void showDialog() {
                    //                    弹出下载dialog
                    FragmentTransaction transaction = fragmentManager.beginTransaction();
                    Bundle              args        = new Bundle();
                    args.putString("id", id);
                    DialogFragment dialogFragment = new DownloadDialogFragment();
                    dialogFragment.setArguments(args);
                    dialogFragment.show(transaction, "dialog");
                }
            });
        }
    }
}
